var util = require('../../utils/util.js')
var app = getApp();
var md5 = require('../md5.js');
Page({
  data: {
    setlanguage:0
  },
  //底部导航跳转
  toAiIndex: function () {
    util.toAiIndex()
  },
  toContactsIng: function () {
    util.toContactsIng()
  },
  toProductRevision: function () {
    util.toProductRevision()
  },
  toMy: function () {
    util.toMy()
  },
  toBadge: function () {
    util.toBadge()
  },
  submit: function () {
    this.setData({
      showModal: true
    })
  },
onPullDownRefresh: function(){
    wx.stopPullDownRefresh({
      complete: (res) => {},
    });
  },
  switchChange: function (e) {
    var that = this
    console.log(e.detail.value)
    var tjhyStatue = e.detail.value+''
    var exhibitionid = wx.getStorageSync('exhibitionid')
    //设置状态值
    var state =''
    if (tjhyStatue=='true'){
      state='1'
    }
    if (tjhyStatue=='false') {
      state = '0'
    }
    var exhibitionid = wx.getStorageSync('exhibitionid')
    var u = wx.getStorageSync('u')
    console.log('exhibitionid=' + exhibitionid)
    console.log('u=' + u)
    if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
      && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
      //默认最新（调用pico接口）
      var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;
      var url = app.globalData.picoaddress + '/attendee/user/setrecommend'
      var k = {}
      k["v"] = '1.0.0'
      k["client"] = 'ios'
      k["timestamp"] = timestamp
      k["lan"] = 'cn'
      k["exhibitionid"] = exhibitionid
      k["u"] = u
      k["state"] = state
      k["opstate"] = '1'
      var sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'opstate1' + 'state' + state + 'timestamp' + timestamp + app.globalData.picotoken
      console.log(sign)
      var curb64 = md5.hexMD5(sign)
      console.log(curb64)
      k["sign"] = curb64
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        url: url,
        method: 'post',
        data: k,
        success(res) {
          console.log(res)
          var mesg = res.data
          if (mesg.code == 200) {
            that.setData({
              tjhyStatue: tjhyStatue
            })
          } 
        }, fail: res => {
          console.log(res, "error")
        }

      })
    } else {
      wx.redirectTo({
        url: '../signInbyPhone/signIn'
      })
    } 
  },

  go: function () {
    this.setData({
      showModal: false
    })
  },
  LogOut: function () {
    var exhibitionId = wx.getStorageSync('exhibitionid');
    var u = wx.getStorageSync('u');
    console.log("u = " + u);
    console.log("exhibitionId = " + exhibitionId);

    if (wx.getStorageSync('exhibitionid' || !"".trim() == wx.getStorageSync('exhibition')) &&
      wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u')) {
      var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;
      var url = app.globalData.picoaddress + '/attendee/user/profile';
      var k = {};
      k["v"] = '1.0.0';
      k["client"] = 'ios';
      k["timestamp"] = timestamp;
      k["lan"] = 'cn';
      k["exhibitionid"] = exhibitionId;
      k["u"] = u;
      var sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionId + 'timestamp' + timestamp + app.globalData.picotoken
      console.log(sign)
      var curb64 = md5.hexMD5(sign)
      console.log(curb64)
      k["sign"] = curb64


      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        url: url,
        method: 'post',
        data: k,
        success(res) {
          //2.清空缓存 
          wx.removeStorageSync({
            key: 'exhibitionid',
            success: function (res) { console.log(res.data) },
          });
          wx.removeStorageSync({
            key: 'u',
            success: function (res) { console.log(res.data) },
          });
          //3.跳转 登录页../signInbyPhone/signIn 
          wx.reLaunch({
            url: '../signInbyPhone/signIn'
          })
        }, fail: res => {
          console.log(res, "error")
        }

      })
    } else {
      wx.redirectTo({
        url: '../signInbyPhone/signIn'
      })
    }
  }, 
  setzw: function () {
    var setlanguage = wx.getStorageSync('setlanguage')
    var title =''
    var content = ''
    var cancelText = ''
    var confirmText = ''
    if (setlanguage==1){
      title = 'Tips'
      content='Changing language setting will reload,continue?'
      cancelText ='No'
      confirmText = 'Yes'
    }else{
      title = '提示'
      content = '更改语言需重新加载，是否继续？'
      cancelText = '取消'
      confirmText = '确定'
    }
    wx.showModal({
      title: title,
      content: content,
      cancelText: cancelText,
      confirmText: confirmText,
      success(res) {
        if (res.confirm) {
          wx.setStorageSync('setlanguage', 0);
          wx.navigateTo({
            url: '../aiIndex/aiIndex',
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

   
  },
  setyw: function () {
    var setlanguage = wx.getStorageSync('setlanguage')
    var title = ''
    var content = ''
    var cancelText = ''
    var confirmText = ''
    if (setlanguage == 1) {
      title = 'Tips'
      content = 'Changing language setting will reload,continue?'
      cancelText = 'No'
      confirmText = 'Yes'
    } else {
      title = '提示'
      content = '更改语言需重新加载，是否继续？'
      cancelText = '取消'
      confirmText = '确定'
    }
    wx.showModal({
      title: title,
      content: content,
      cancelText: cancelText,
      confirmText: confirmText,
      success(res) {
        if (res.confirm) {
          wx.setStorageSync('setlanguage', 1);
          wx.navigateTo({
            url: '../aiIndex/aiIndex',
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  onLoad: function (options) {
    var that = this
    //获取状态
    var exhibitionid = wx.getStorageSync('exhibitionid')
    var u = wx.getStorageSync('u')
    console.log('exhibitionid=' + exhibitionid)
    console.log('u=' + u)
    if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
      && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
      //默认最新（调用pico接口）
      var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;
      var url = app.globalData.picoaddress + '/attendee/user/setrecommend'
      var k = {}
      k["v"] = '1.0.0'
      k["client"] = 'ios'
      k["timestamp"] = timestamp
      k["lan"] = 'cn'
      k["exhibitionid"] = exhibitionid
      k["u"] = u
      k["opstate"] = '0'
      var sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'opstate0' + 'timestamp' + timestamp + app.globalData.picotoken
      console.log(sign)
      var curb64 = md5.hexMD5(sign)
      console.log(curb64)
      k["sign"] = curb64
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        url: url,
        method: 'post',
        data: k,
        success(res) {
          console.log(res)
          var mesg = res.data
          var curdata = mesg.data
          var state = curdata.state
          console.log('state=' + state)
          if (state == '1') {
            that.setData({
              tjhyStatue: 'true'
            })
          }
          if (state == '0') {
            that.setData({
              tjhyStatue: 'false'
            })
          }
        }, fail: res => {
          console.log(res, "error")
        }

      })
    } else {
      wx.redirectTo({
        url: '../signInbyPhone/signIn'
      })
    } 
      
    
    //设置语言0:中文，1：英文
    var setlanguage = wx.getStorageSync('setlanguage')
    if ((wx.getStorageSync('setlanguage') || !"".trim() == wx.getStorageSync('setlanguage'))) {
      this.setData({
        setlanguage: wx.getStorageSync('setlanguage')
      })
      if (setlanguage == 1) {
        wx.setNavigationBarTitle({
          title: 'Setting'
        })
      } else {
        wx.setNavigationBarTitle({
          title: '设置'
        })
      }
    } else {
      wx.setStorageSync('setlanguage', 0);
      this.setData({
        setlanguage: 0
      })
    }
  },
})